/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.absisgroup.nt;

import com.absisgroup.nsc.utilities.GenericServlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

/**
 *
 * @author Hue
 */
public class TaoMoiKho extends GenericServlet {

    @Override
    public int process(final Connection con, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
        save(con, request);
        con.commit();
        
        final JSONObject jsObj = new JSONObject();
        jsObj.put("success", true);
        
        writeToResponse(response, jsObj.toString());
        
        return DONT_RUN_JSP;
    }
    
    private void save(final Connection con, final HttpServletRequest request) throws Exception {
        String strQuery = "insert into WAREHOUSE(WH_NAME, WH_ADDRESS, WH_PHONE, WH_TYPE) values(?, ?, ?, ?)";
        PreparedStatement pStmt = con.prepareStatement(strQuery);
        int iIndex = 1;
        pStmt.setString(iIndex++, request.getParameter("name"));
        pStmt.setString(iIndex++, request.getParameter("address"));
        pStmt.setString(iIndex++, request.getParameter("phone"));
        pStmt.setString(iIndex++, request.getParameter("type"));
        pStmt.executeUpdate();
        pStmt.close();
    }
    
    @Override
    protected boolean useConnection() {
        return true;
    }
}
